<?php

declare(strict_types=1);

namespace app\common\model;
use think\facade\Db;

class Practice extends Base
{

    protected $name = 'practice';
    /**
     * 试卷生成
     */
    public static function createExam($param, $user)
    {
        try {
            $crate_type = 1; //1已生成，2重新生成
            $endtime = time() + 3600 * 2;
            $cate = Cate::where('id', $user->cate)->find();
            if (empty($cate)) {
                return ['code' => -1, 'msg' => '暂无课程类型'];
            }
            $info = parent::where(['uid' => $user->id])->order('create_time desc')->find();
            $data = [
                'uid' => $user->id,
                'title' => $user->cateText . '练习试卷',
                'end_time' => $endtime,
                'radio_num' => $cate['radio_num'],
                'radio_score' => $cate['radio_score'],
                'check_num' => $cate['check_num'],
                'check_score' => $cate['check_score'],
            ];
            if (empty($info)) {
                $crate_type = 2;
                $res = parent::create($data);
            } else {
                if (isset($param['type']) && $param['type'] == 1) {
                    $crate_type = 2;
                    $info->status = 1;
                    $info->score = 0;
                }else{
                    if ($info->status == 1) {
                        $crate_type = 2;
                    }
                }
                $info->end_time = $endtime;
                $info->title = $user->cateText . '练习试卷';
                $info->radio_num = $cate['radio_num'];
                $info->radio_score = $cate['radio_score'];
                $info->check_num = $cate['check_num'];
                $info->check_score = $cate['check_score'];
                $info->save();
                $res = $info;
            }
            // print_r($crate_type);die;
            $resbank = PracticeBank::createBank($res, $user, $crate_type);
            // print_r($resbank);die;
            if ($resbank['code'] == 1) {
                return ['code' => 1, 'msg' => '生成成功！'];
            } else {
                return ['code' => -1, 'msg' => $resbank['msg']];
            }
        } catch (\Exception $e) {
            return ['code' => -1, 'msg' => $e->getMessage()];
        }
    }
    /**
     * 获取当前题目
     */
    public static function getTopic($id){
        try {
            $info = Exam::where(['id'=>$id])->find();
            $list = Db::query("SHOW FULL COLUMNS FROM exam");
            $data = [];
            foreach($list as $k=>$value){
                if(in_array($value['Field'],['A','B','C','D','E'])){
                    if(!empty($info[$value['Field']])){
                        $data[]=[
                            'key'=> $value['Field'],
                            'value'=> $value['Field'].'.'.$info[$value['Field']],
                        ];
                    }
                }
            }
            $list = [
                'id'=>$info['id'],
                'title'=>$info['title'],
                'images'=>$info['images'],
                'list'=>$data,
                'my_answer'=>'',
                'answer'=>'',
                'parse'=>'',
            ];
            return $list;
        } catch (\Exception $e) {
            return ['code' => -1, 'msg' => $e->getMessage()];
        }
    }
}
